<template>
  <div class="card-container">
    <el-form :model="form" ref="formRef" label-width="100px" style="width: 100%" :rules="formRules">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="clearfix">
          <span>基本信息</span>
        </div>
        <el-form-item label="分类名称：" prop="title">
          <el-input v-model="form.title" placeholder="请输入分类名称"></el-input>
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input v-model.number="form.sort" placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item label="启用：" prop="is_show">
          <el-switch v-model="form.is_show" active-value="1" inactive-value="0" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
        </el-form-item>
      </el-card>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { editCate, getCateDetail } from '@/api/category'
export default {
  data() {
    return {
      form: {
        title: '',
        sort: '',
        is_show: '1',
      },
      formRules: {
        title: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入分类排序', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getCateDetail()
  },
  methods: {
    async getCateDetail() {
      const res = await getCateDetail(this.$route.query.id)
      if (res.code != 1) return this.$message.error(res.msg)
      this.form = res.data
    },
    submitForm() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        const res = await editCate(this.form)
        if (res.code != 1) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        this.closePage()
      })
      // console.log(this.form)
    },
    async closePage() {
      let view = this.$route
      await this.$store.dispatch('tagsBar/delRoute', view)
      this.$router.push('/siteCategory')
    },
    resetForm() {
      this.form = this.$options.data().form
    },
  },
  computed: {},
}
</script>

<style lang="scss" scoped>
.add-cate-container {
  height: 700px;
}
::v-deep .el-input__inner {
  // margin-left: 20px;
  height: 36px;
  line-height: 36px;
}
.el-form-item {
  width: 420px;
}
.el-card {
  border-radius: 8px;
}
::v-deep .el-card__header {
  font-size: 14px;
  color: #555;
  font-weight: bold;
}
</style>
